var request = require('../../utils/request.js')
var app = getApp();
Page({
  data: {
    discountslist:[], 
  },
  onLoad(){
    my.getNetworkType({
      success: (res) => {
        if(!res.networkAvailable||res.networkType=='UNKNOWN'){
          my.showToast({
            content: '网络状态不佳，请检查网络',
            type:'fail',
            duration: 2000
          })
        }
      }
    })
    this.getuserinfo()
  },
  //获取用户优惠券信息
  getuserinfo() {
    let success = (res) => {
      if(res.discounts){
        if (res.discounts) {
          this.data.discountslist = res.discounts
          for (let i = 0; i < this.data.discountslist.length; i++) {
            // if (this.data.discountslist[i].applyType==1){
            if (this.data.discountslist[i].typeId == 0) {
              this.data.discountslist[i].discount = this.data.discountslist[i].value * 10 + '折'
            }
            if (this.data.discountslist[i].typeId == 1) {
              this.data.discountslist[i].discount = this.data.discountslist[i].value + '元'
            }
            // }
            if (this.data.discountslist[i].expireType == 0) {
              this.data.discountslist[i].expireTime = this.data.discountslist[i].endTime
            } else if (this.data.discountslist[i].expireType == 1) {
              this.data.discountslist[i].expireTime = this.data.discountslist[i].recivedTime
            }
          }
          this.setData({
            discountslist: this.data.discountslist
          })
        }
      }
      
    }
    request.getdata("/users/user/show", {}, success, this);
  },
  //不使用优惠券
  coupon_btn(){
    my.setStorageSync({
      key: "selectDiscount",
      data: null
    });
    my.navigateBack({
      delta: 1
    })
    let pages = getCurrentPages();//当前页面
    let prevPage = pages[pages.length-2];//上一页面
    prevPage.setData({//直接给上一页面赋值
        isBack: true
    });
  },
  // 点击使用优惠券
  coupon_user(e){
    my.setStorageSync({
      key: "selectDiscount",
      data: e.currentTarget.dataset.discountitem
    });
    my.navigateBack({
      delta: 1
    });
    let pages = getCurrentPages();//当前页面
    let prevPage = pages[pages.length-2];//上一页面
    prevPage.setData({//直接给上一页面赋值
        isBack: true
    });
  }
})
